package com.share.web.action.sysmgr.privilege;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.share.bean.sysmgr.privilege.PrivilegeResource;
import com.share.bean.sysmgr.privilege.PrivilegeRole;
import com.share.service.sysmgr.privilege.PrivilegeResourceItemManager;
import com.share.service.sysmgr.privilege.PrivilegeResourceManager;
import com.share.service.sysmgr.privilege.PrivilegeRoleManager;
import com.share.web.action.base.BaseActionSupport;


@Component
@Scope("prototype")
public class PrivilegeRoleAction extends BaseActionSupport implements
		ServletRequestAware {

	private static final long serialVersionUID = 1L;

	private PrivilegeRole roleVo;

	@Resource
	private PrivilegeRoleManager roleMgr;

	@Resource
	private PrivilegeResourceItemManager resItemMgr;

	private HttpServletRequest request;
	
	@Resource
	private PrivilegeResourceManager resMgr ;

	public void setResMgr(PrivilegeResourceManager resMgr) {
		this.resMgr = resMgr;
	}

	private String type;

	private String[] resId;

	public String creareRole() throws Exception {
		if ("EAGER".equals(type)) {
			
			request.getSession().setAttribute("roles", roleVo);
			request.getSession().setAttribute("resItem",this.resItemMgr.queryResourceItemManager());
			
			return "roleAddRes";
		} else if ("LAZY".equals(type)) {
			this.roleMgr.saveRoleManager(roleVo);
		}
		return "createSuc";
	}
	
	public String queryAllRole() throws Exception {

		request.setAttribute("queryRole", this.roleMgr.queryRoleManager());

		return "queryAllRole";
	}

	public String deleteByIdlRole() throws Exception {
		
		this.roleMgr.deleteRoleManager(roleVo.getRoleId()) ;

		return "loadAllRole";
	}

	public String queryByIdRole() throws Exception {

		request.setAttribute("queryByIdRole", this.roleMgr.queryByIdRoleManager(roleVo.getRoleId()));

		if ("queryUpdateByIdRole".equals(type)) {
			request.getSession().setAttribute("resList",this.resMgr.queryResourceManager());
			type = null;
			return "queryUpdateByIdRole";
		} else {
			return "queryByIdRole";
		}
	}

	@SuppressWarnings("unchecked")
	public String updateByIdRole() throws Exception {
		try {
			Set set = new HashSet();
			if (null != resId && resId.length > 0) {
				for (int i = 0; i < resId.length; i++) {
					PrivilegeResource r = new PrivilegeResource();
					r.setResId(Integer.parseInt(resId[i]));
					set.add(r);
				}
				roleVo.setResource(set);
			}
			this.roleMgr.updateRoleManager(roleVo);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "pub_add_success";
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public String[] getResId() {
		return resId;
	}

	public PrivilegeRole getRoleVo() {
		return roleVo;
	}

	public String getType() {
		return type;
	}

	@SuppressWarnings("unchecked")
	public String roleAddRes() throws Exception {

		Set set = new HashSet();

		roleVo = (PrivilegeRole) request.getSession().getAttribute("roles");

		for (int i = 0; i < resId.length; i++) {
			PrivilegeResource res = new PrivilegeResource();
			res.setResId(Integer.parseInt(resId[i]));
			set.add(res);
		}
		roleVo.setResource(set);

		this.roleMgr.saveRoleManager(roleVo);

		request.getSession().removeAttribute("roles");
		request.getSession().removeAttribute("res");
		roleVo = null;

		return "loadAllRole";
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setResId(String[] resId) {
		this.resId = resId;
	}

	public void setResItemMgr(PrivilegeResourceItemManager resItemMgr) {
		this.resItemMgr = resItemMgr;
	}

	public void setRoleMgr(PrivilegeRoleManager roleMgr) {
		this.roleMgr = roleMgr;
	}

	public void setRoleVo(PrivilegeRole roleVo) {
		this.roleVo = roleVo;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setType(String type) {
		this.type = type;
	}

}
